# bouncing_lines.py
#
# by Matthew Weathers, to play with gyGame module, adapted from C++ examples in Fall 2005
# CSCI 105 class at Biola University.
# 27-Dec-2009 - Bouncing Lines, using lists

#Import Modules
import pygame, sys, time, random
from pygame.locals import *
from bgi_colors import * # imports 64 colors in BGIcolors, plus BLACK, BLUE...

#functions

#classes for screen objects

class MovingPoint:
    """A moving point with location and speed, automatically bounces.
    This version has a history of locations."""
    def __init__(self, maxx=640, maxy=480, initx=0, inity=0,
                 initxspeed=0, inityspeed=0):
        self.queuesize = 50
        self.x = initx
        self.y = inity
        self.xv = initxspeed # xv stands for "X Velocity"
        self.yv = inityspeed
        self.maxx = maxx
        self.maxy = maxy
        self.pxqueue = [None for i in range(self.queuesize)]
        self.pyqueue = [None for i in range(self.queuesize)]
        self.queueptr = 0;


    def update(self):
        self.prevx = self.pxqueue[self.queueptr]
        self.prevy = self.pyqueue[self.queueptr]
        self.pxqueue[self.queueptr] = self.x
        self.pyqueue[self.queueptr] = self.y
        self.queueptr = (self.queueptr + 1) % self.queuesize
        self.x += self.xv
        self.y += self.yv
        if (self.x<0 or self.x>self.maxx):
            self.xv *= -1
        if (self.y<0 or self.y>self.maxy):
            self.yv *= -1
        #Sanity check, if they're way off, reset to middle
        if (self.x<-20 or self.y<-20 or self.x>self.maxx+20 or
            self.y>self.maxy+20):
            self.x = self.maxx/2
            self.y = self.maxy/2

def main():
    """this function is called when the program starts.
       it initializes everything it needs, then runs in
       a loop until the function returns."""
# Initialize pygame, window
    pygame.init()
    mainClock = pygame.time.Clock()
    WINDOWWIDTH = 800
    WINDOWHEIGHT = 600
    windowSurface = pygame.display.set_mode((WINDOWWIDTH, WINDOWHEIGHT), 0, 32)
    pygame.display.set_caption('Bouncing Lines - MDW')

# Fill screen with a color
    windowSurface.fill(LIGHTBLUE)

# Write a rectangle of text
    basicFont = pygame.font.SysFont(None, 48)
    text = basicFont.render('Bouncing Lines', True, WHITE, BLUE)
    textRect = text.get_rect()
    textRect.centerx = windowSurface.get_rect().centerx
    textRect.centery = windowSurface.get_rect().centery
    windowSurface.blit(text, textRect)

    # pygame.draw.lines(windowSurface, BLACK, True, [(0, 0), (400, 700)])
    
    pygame.display.update()

# Wait for a mouseclick down, then continue
    print str(pygame.event.get()) # Toss all current events
    while pygame.event.get(MOUSEBUTTONDOWN) == []:
        time.sleep(0.2)

# Fill screen with a color
    windowSurface.fill(BLACK)
    pygame.display.update()

# Prepare Objects
    boxes = []
    for i in range(10):
        boxes.append(MovingPoint(WINDOWWIDTH,WINDOWHEIGHT,
                                 random.randint(0,WINDOWWIDTH-10),
                                 random.randint(0,WINDOWHEIGHT-10),
                                 random.uniform(-7.0, 7.0),
                                 random.uniform(-7.0, 7.0)))

# Initial Settings
    gravityOn = False
    tailsOn = False
    nextColor = 1
    counter = 0

# MAIN GAME LOOP
    while True:
        mainClock.tick(40) # Never run more than 20 FPS

    #Handle Input Events
        for event in pygame.event.get():
            if event.type == QUIT:
                return
            elif event.type == KEYUP:
                if event.key == K_ESCAPE:
                    return
                if event.key == ord('g'):
                    gravityOn = not gravityOn
                if event.key == ord('t'):
                    tailsOn = not tailsOn
                if event.key == ord('c'):
                    windowSurface.fill(BLACK)
    #Update objects
        for i in range(len(boxes)):
            boxes[i].update()

        pointlist = [(boxes[i].prevx, boxes[i].prevy) for i in
                     range(len(boxes)) if boxes[i].prevx != None]
        if len(pointlist)>1:
            pygame.draw.lines(windowSurface, BLACK, True, pointlist)

        pointlist = [(boxes[i].x, boxes[i].y) for i in range(len(boxes))]
        counter = counter + 1
        if counter > 30:
            counter = 0
            nextColor = (nextColor + 1 ) % 64
        pygame.draw.lines(windowSurface, BGIcolors[nextColor], True, pointlist)
            
        pygame.display.update()
        

        
    # END GAME LOOP


#Game Over, end of main()

#this calls the 'main' function when this script is executed
if __name__ == '__main__':
    main()
    #Quit everything
    pygame.quit()
    sys.exit()

